<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Exception;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class InformationComment extends Api
{

    /**
     * Comment模型对象
     * @var \app\admin\model\information\Comment
     */
    protected $model = null;

    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\information\Comment;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 评论列表
     */
    public function index()
    {
        $params = $this->request->param();
        try {
            $where = [
                'information_id' => $params['information_id'],
            ];
            $list = $this->model->where($where)->with('user')->paginate($params['page_size'] ?? 20);

            foreach ($list as $item) {
                $item['createtime_text'] = date('Y-m-d H:i:s', $item['createtime']);
            }

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('查询成功', $list);
    }

    /**
     * 新增评论
     */
    public function add()
    {
        $params = $this->request->post();
        try {

            //是否采用模型验证
            $validate_class = '\app\admin\validate\information\Comment';
            $validate = validate($validate_class);
            if (!$validate->check($params, [], "add")) {
                throw new Exception($validate->getError());
            }

            $params['user_id'] = $this->auth->id;
            $this->model->allowField(true)->save($params);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('新增成功', $params);
    }

    /**
     * 新增评论
     */
    public function delete()
    {

    }

}
